home *** CD-ROM | disk | FTP | other *** search
- Path: news.chalmers.se!usenet
- From: Eivind Nordby <Eivind.Nordby@hks.se>
- Newsgroups: comp.lang.c++
- Subject: Re: Using cin like sscanf()?
- Date: Wed, 06 Mar 1996 13:37:36 +0100
- Organization: University of Karlstad, Dept of Computer Science
- Message-ID: <313D8710.1CFB@hks.se>
- References: <4gnudd$dd4@daily-planet.nodak.edu>
- NNTP-Posting-Host: eivind.cs.hks.se
- Mime-Version: 1.0
- Content-Type: text/plain; charset=us-ascii
- Content-Transfer-Encoding: 7bit
- X-Mailer: Mozilla 2.0 (X11; I; AIX 2)
- CC: ishaq@plains.nodak.edu
-
- Jeffro wrote:
- >
- > Say I input a string into string:
- >
- > char string[] = "This is a character string.";
- > char buffer[24];
- >
- > //cin.read_from(string)
- > while (cin >> buffer) cout << buffer; //Outputs string
- > //cin.read_from(stdin)
- >
- > This should be similar to:
- >
- > while (sscanf(string, "%s", &buffer)) cout << buffer;
- >
- > Any ideas?
- > Thanks!
- > -jeff
- > --
- > .,;';.,.;';.,.;';.,.;';.,.;';.,.;';.,.;';.,.;';.,.;';.,.;';.,.;';.,.;';.,.
- > - Jeff Ishaq ishaq@plains.nodak.edu Computer Science major -
- > - at North Dakota State University in Fargo, ND. Drop me a line, eh? -
-
- C++ has a class istrstream for this purpose. You need to declare a istrstream
- variable on the input string string. With your example:
-
- #include <strstream.h>
- :
- {
- istrstream sin(string);
- while (sin >> buffer) cout << buffer;
- }
-
- This program fragment will display Thisisacharacterstring.
- The braces make the sin variable vanish once it has done its job.
-
- Good luck
- Eivind
- --
- _____________________________________________________________________________
- Dept of Computer Science * University of Karlstad * 651 88 Karlstad * Sweden
- Eivind J. Nordby * Eivind.Nordby@hks.se; http://f-server.cs.hks.se:80/~eivind/
- Voice: +46-(0)54-83 81 53 (mo, we, fr) * Fax: +46-(0)54-83 84 46
-